<?php

namespace app\adminapi\controller\setting\lang;

use app\adminapi\controller\BaseAdminApiController;
use app\adminapi\lists\setting\lang\TranslateLists;
use app\adminapi\logic\setting\lang\TranslateLogic;
use app\adminapi\validate\setting\lang\TranslateValidate;

/**
 * 语言翻译控制器
 * Class LanguasController
 * @package app\adminapi\controller\setting
 */
class TranslateController extends BaseAdminApiController
{

    public $notNeedLogin = ['getClientOptions','autoTranslate', 'buildLangPack', 'download'];

    /**
     * @notes 语言翻译列表
     * @return \support\Response
     */
    public function lists()
    {
        return $this->dataLists(new TranslateLists());
    }

    /**
     * 获取所有选项
     * @return \support\Response
     */
    public function getClientOptions()
    {
        $result = TranslateLogic::getClientOptions();
        return $this->data($result);
    }


    /**
     * @notes 查看语言详情
     * @return \support\Response
     */
    public function detail()
    {
        $params = (new TranslateValidate())->goCheck('detail');
        $result = TranslateLogic::detail($params);
        return $this->data($result);
    }

    /**
     * @notes 添加
     * @return \support\Response
     */
    public function add()
    {
        $params = (new TranslateValidate())->post()->goCheck('add');
        $result = TranslateLogic::add($params);
        if (true === $result) {
            return $this->success('添加成功', [], 1, 1);
        }
        return $this->fail(TranslateLogic::getError());
    }


    /**
     * @notes 编辑
     * @return \support\Response
     */
    public function edit()
    {
        $params = (new TranslateValidate())->post()->goCheck('edit');
        $result = TranslateLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(TranslateLogic::getError());
    }

    /**
     * @notes 删除
     * @return \support\Response
     */
    public function delete()
    {
        $params = (new TranslateValidate())->post()->goCheck('delete');
        $result = TranslateLogic::delete($params);
        if (true === $result) {
            return $this->success('删除成功', [], 1, 1);
        }
        return $this->fail(TranslateLogic::getError());
    }

    /**
     * 自动翻译接口
     * @return \support\Response
     */
    public function autoTranslate()
    {
        $params = (new TranslateValidate())->post()->goCheck('autoTranslate');
        $result = TranslateLogic::autoTranslate($params);
        if (false === $result) {
            return $this->fail(TranslateLogic::getError());
        }
        return $this->success('操作成功', $result, 1, 1);
    }

    /**
     * 生成语言包
     * @return \support\Response
     */
    public function buildLangPack()
    {
        $result = TranslateLogic::buildLangPack();
        if (false === $result) {
            return $this->fail(TranslateLogic::getError());
        }
        return $this->success('操作成功', $result, 1, 1);
    }

    /**
     * 下载语言包
     * @return \support\Response|\Webman\Http\Response
     */
    public function download()
    {
        $params = (new TranslateValidate())->get()->goCheck('download');
        $result = TranslateLogic::download($params['file']);
        if (false === $result) {
            return $this->fail(TranslateLogic::getError() ?: '下载失败');
        }
        return \response()->download($result, $params['file']);
    }
}
